package com.hbzhit.modules.wx.controller;

import com.alibaba.fastjson.JSONObject;
import com.hbzhit.common.utils.R;
import com.hbzhit.modules.sys.entity.SysUserEntity;
import com.hbzhit.modules.sys.service.SysRoleService;
import com.hbzhit.modules.sys.service.SysUserService;
import com.hbzhit.modules.wx.service.WxUserService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@AllArgsConstructor
@RestController
@RequestMapping(value = "/wx/user")
public class WxUserController {

    private final SysUserService userService;
    private final SysRoleService roleService;
    @Resource
    private WxUserService wxUserService;

    /**
     * 更具微信openId 查询管理员信息
     *
     * @param openId 微信openId
     */
    @GetMapping(value = "/getUser/{openId}")
    public R<JSONObject> getUser(@PathVariable String openId) {
        SysUserEntity user = userService.queryByOpenId(openId);
        JSONObject data = new JSONObject();
        data.put("user", user);
//        if (user != null) {
//            List<SysRoleEntity> roleList = roleService.queryListByUserId(user.getUserId());
//            Set<String> permissions = shiroService.getUserPermissions(user.getUserId());
//            data.put("role", roleList.get(0));
//            data.put("permissions", permissions);
//        }
        return R.ok(data);
    }

    /**
     * 绑定管理用户的微信openId
     *
     * @param params 绑定查询参数
     */
    @PostMapping(value = "/bindUser")
    public R<Object> bindUser(@RequestBody Map<String, Object> params) {
        userService.bindOpenId(params);
        return R.ok();
    }

    /**
     * 解除微信openId绑定
     *
     * @param user 解绑用户
     */
    @PostMapping(value = "/unbindUser")
    public R<Object> unbindUser(@RequestBody SysUserEntity user) {
        userService.unbindOpenId(user);
        return R.ok();
    }

    @SneakyThrows
    @GetMapping("/getAccessToken")
    @ApiOperation("获取企业微信token")
    public R<Object> getAccessToken() {
        return R.ok().data(wxUserService.getAccessToken());
    }

}
